﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PriceAdjustPlanAdd.cs
    // 文件功能描述：新增价格调整计划
    // 创建标识：吕海乐2009-04-08
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class AreaPriceAdjustPlanAdd : BasePage
    {
        public AreaPriceAdjustPlanAdd()
        {
            //this.ModuleNo = Module.产品调价.ToString();

        }
        private string procureCatalogId = "";
        private string ProjectID = "";
        private string AreaID = "";
        private decimal ProcurePrice = 0;

        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取需要新增调价计划的采购产品的信息
            if (!this.IsPostBack)
            {
                if (Request["procureCatalogId"] == null)
                {
                    PageError("参数错误！", this.ReturnUrl);
                    return;
                }
                if (Request["ProjectID"] == null)
                {
                    PageError("参数错误！", this.ReturnUrl);
                    return;
                }
                if (Request["ProcurePrice"] == null)
                {
                    PageError("参数错误！", this.ReturnUrl);
                    return;
                }
                if (Request["Price"] == null)
                {
                    PageError("参数错误！", this.ReturnUrl);
                    return;
                }

                procureCatalogId = Request.QueryString["procureCatalogId"].ToString();
                ProjectID = Request.QueryString["ProjectID"].ToString();
                ProcurePrice = Convert.ToDecimal(Request.QueryString["ProcurePrice"]);
                ViewState["ProcurePrice"] = ProcurePrice;

                //if (Request["SortSource"] == "3")
                //{
                //    PageError("备案采购不需要设置实际采购价！", this.ReturnUrl);
                //    return;
                //} 

                if (CurrentAccount.GetUser().UserID != "ROOT" && CurrentAccount.GetUser().UserRole.RoleID == "03")
                {
                    IBLL_ProcurementBasic ibb = Factory.BLL_ProcurementBasic();
                    //读取TB_ManageOrg表查看 权限
                    DataTable dt = ibb.GetTB_ManageOrg(CurrentAccount.GetUser().UserID);
                    if (dt.Rows.Count > 0)
                    {
                        AreaID = dt.Rows[0]["AreaID"].ToString();
                        this.txtArea.Text = dt.Rows[0]["ManageOrgName"].ToString();

                        ViewState["AreaID"] = AreaID;

                        if (dt.Rows[0]["CatalogState"].ToString() != "1")
                        {
                            PageError("没有权限修改实际采购价！", this.ReturnUrl);
                            return;
                        }

                        if (dt.Rows[0]["CatalogType"].ToString() == "")
                        {
                            PageError("没有权限修改实际采购价！", this.ReturnUrl);
                            return;
                        }
                        else
                        {
                            string IsBasicRemedy = Request.QueryString["IsBasicRemedy"].ToString();
                            if (!dt.Rows[0]["CatalogType"].ToString().Contains(IsBasicRemedy))
                            {
                                PageError("没有权限修改实际采购价！", this.ReturnUrl);
                                return;
                            }

                        }
                    }
                }
                else
                {
                    PageError("没有权限修改实际采购价！", this.ReturnUrl);
                    return;
                }

                Model.View.ProcureCatalog procureCatalog = ipb.GetProcureCatalogByID(procureCatalogId);
                if (procureCatalog != null)
                {   
                    txtCompany_SC.Text = procureCatalog.CompanyName_SC;
                    txtCompany_TB.Text = procureCatalog.CompanyName_TB;
                    txtDateTime.Text = string.Format("{0:d}", procureCatalog.AddTime);
                    txtGoodsName.Text = procureCatalog.GoodsName;
                    txtMaterial.Text = procureCatalog.Material;
                    txtOutLook.Text = procureCatalog.Outlookc;
                    //txtSortSource.Text = HZ.DrugTrade.Web.Function.FormatDrugSource(procureCatalog.SortSource);
                    //txtPack.Text = procureCatalog.Pack;

                    txtProcurePrice.Text = Request["Price"].ToString().Substring(0,Request["Price"].ToString().Length -1);;
                    txtProject.Text = procureCatalog.ProjectName;
                    txtUnit.Text = procureCatalog.Unit;
                    txtExecuteStartTime.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:00:00");

                    //添加属性
                    txtExecuteStartTime.Attributes.Add("onFocus", "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false,readOnly:true,minDate:'" + DateTime.Now.AddHours(0.5).ToString("yyyy-MM-dd HH:mm:ss") + "'})");
                }
                else
                {
                    PageError("采购产品不存在！", this.ReturnUrl);
                }
            }
        }

        /// <summary>
        /// 增加价格调整计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (string.IsNullOrEmpty(txtAdjustPrice.Text.Trim()))
                {
                    PageError("调整价格不能为空！", "");
                    return;
                }
                if (this.ddlExecuteWay.SelectedIndex == 0)
                {
                    if (ViewState["ProcurePrice"] != "" && txtAdjustPrice.Text != "")
                    {
                        if (Convert.ToDecimal(ViewState["ProcurePrice"]) < Convert.ToDecimal(txtAdjustPrice.Text))
                        {
                            PageError("实际采购价格不能大于采购限价！", "");
                            return;
                        }
                    }

                    //立即调整价格
                    ImmediateExecute();
                }
                else
                {
                    //定时执行价格调整
                    ScheduleExecute();
                }
            }
        }

        /// <summary>
        /// 立即调整价格
        /// </summary>
        private void ImmediateExecute()
        {
            //
            AdjustPricePlan plan = new AdjustPricePlan()
            {
                AdjustPrice = new HZ.Common.ConvertHelper(txtAdjustPrice.Text.Trim()).ToDecimal,
                AdjustPriceReason = txtAdjustPriceReason.Text.Trim(),
                OriginalPrice = string.IsNullOrEmpty(txtProcurePrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtProcurePrice.Text.Trim()).ToDecimal,
                ExecuteStartTime = DateTime.Now,
                PlanPerson = CurrentAccount.GetUser().UserID,
                PlanTime = DateTime.Now,
                PricePlanSerialID = Guid.NewGuid().ToString(),
                ProcureCatalogID = Request["procureCatalogId"].ToString(),
                ExecuteState = 1,
                ExecuteWay = 0
            };

            ProjectID = Request["ProjectID"].ToString();
            AreaID = ViewState["AreaID"].ToString();
            int flag = ipb.UpdateAreaProcureCatalog_Price(plan, ProjectID, AreaID); 
            if (flag == 0)
            {
                PageError("修改失败！", "");
            }
            else if (flag == 1)
            {
                PageInfo("修改成功！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 定时执行价格调整
        /// </summary>
        private void ScheduleExecute()
        {
            if (string.IsNullOrEmpty(txtExecuteStartTime.Text.Trim()))
            {
                PageError("执行时间不能为空！", this.ReturnUrl);
                return;
            }

            AdjustPricePlan plan = new AdjustPricePlan()
            {
                AdjustPrice = new HZ.Common.ConvertHelper(txtAdjustPrice.Text.Trim()).ToDecimal,
                AdjustPriceReason = txtAdjustPriceReason.Text.Trim(),
                OriginalPrice = string.IsNullOrEmpty(txtProcurePrice.Text.Trim()) ? 0 : new HZ.Common.ConvertHelper(txtProcurePrice.Text.Trim()).ToDecimal,
                ExecuteStartTime = new HZ.Common.ConvertHelper(txtExecuteStartTime.Text.Trim()).ToDateTime,
                PlanPerson = CurrentAccount.GetUser().UserID,
                PlanTime = DateTime.Now,
                PricePlanSerialID = Guid.NewGuid().ToString(),
                ProcureCatalogID = Request["procureCatalogId"].ToString(),
                ExecuteState = 0,
                ExecuteWay = 1
            };

            //执行方法
            if (ipb.AddAdjustPricePlan(plan) == 1)
            {
                PageInfo("添加成功！", this.ReturnUrl);
            }
            else
            {
                PageError("添加失败！", this.ReturnUrl);
            }
        }


    }
}
